//
//  TimeStampTool.m
//  WeCommunity
//
//  Created by mac on 2017/6/15.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "TimeStampTool.h"

@implementation TimeStampTool

+(NSString *)formatDateWithDynamicTime:(id )oldDate
{
    NSTimeInterval nowTimestamp = [[NSDate date] timeIntervalSince1970];
    NSString *tempStr = [NSString stringWithFormat:@"%@",oldDate];
    NSTimeInterval _interval=0;
    if (tempStr.length == 13) {
        _interval=[oldDate longLongValue]/1000;
    }else
    {
        _interval=[oldDate longLongValue];
    }
    NSDate *oldfromTimesp = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSDate *nowDate=[NSDate date];
    NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
    
    [formatter setDateFormat:@"YYYY"];
    NSString *oldStr=[formatter stringFromDate:oldfromTimesp];
    NSString *nowStr=[formatter stringFromDate:nowDate];
    if (oldStr.intValue-nowStr.intValue!=0) {  // 不是同一年的 显示为x年x月x日
        [formatter setDateFormat:@"YYYY-MM-dd"];
        NSString *oldStr=[formatter stringFromDate:oldfromTimesp];
        return oldStr;
    }else{  // 是同一年的
        
        [formatter setDateFormat:@"MM"];
        oldStr=[formatter stringFromDate:oldfromTimesp];
        nowStr=[formatter stringFromDate:nowDate];
        if (abs(oldStr.intValue-nowStr.intValue) == 0) { //同一个月份
            [formatter setDateFormat:@"dd"];
            oldStr=[formatter stringFromDate:oldfromTimesp];
            nowStr=[formatter stringFromDate:nowDate];
            if (oldStr.intValue-nowStr.intValue!=0) { //时间不是今天的时间：
                if (abs(oldStr.intValue-nowStr.intValue)<=1) { //昨天的时间，显示“昨天 12:03”，不涉及到秒；
                    [formatter setDateFormat:@"HH:mm"];
                    return [NSString stringWithFormat:@"%@ %@",KNSLocal(@"yesterday"),[formatter stringFromDate:oldfromTimesp]];
                }else if (abs(oldStr.intValue-nowStr.intValue)<=2){ //前天的时间，显示“前天 12:03”，不涉及到秒；
                    [formatter setDateFormat:@"HH:mm"];
                    return [NSString stringWithFormat:@"%@ %@",KNSLocal(@"beforeYesterday"),[formatter stringFromDate:oldfromTimesp]];
                }else{ //时间再久的，本年之内显示，月日，比如”4-12”;
                    [formatter setDateFormat:@"MM-dd"];
                    NSString *oldStr=[formatter stringFromDate:oldfromTimesp];
                    return oldStr;
                }
            }else{  //今天的时间：
                
                NSTimeInterval gapValue = nowTimestamp - _interval; // 时间差 单位秒
                if (gapValue/(3600 * 24) <= 1) { //一天内
                    if (gapValue < 60) { //小于3秒钟的，显示“刚刚”；(修改，1分钟内显示为刚刚)
                        return KNSLocal(@"now");
                    }
                    else if (gapValue >= 60 && gapValue < 60*60 ){ //1分钟 - 59分钟的，显示“x分钟之前”；
                        return [NSString stringWithFormat:@"%.0f%@",gapValue/60,KNSLocal(@"minutesAgo")];
                    }
                    else if (gapValue >= 60*60 && gapValue < 60 * 60 * 24 ){ //1小时 - 23小时的，显示“x小时之前
                        return [NSString stringWithFormat:@"%.0f%@",gapValue/(60 * 60),KNSLocal(@"hoursAgo")];
                    }
                }
            }
        }else{  //不是同一个月份
            [formatter setDateFormat:@"MM-dd"];
            NSString *oldStr=[formatter stringFromDate:oldfromTimesp];
            return oldStr;
        }
    }
    return nil;
}

+(NSString *)formatTime:(id)time WithType:(NSString *)tpye
{
    NSString *tempStr = [NSString stringWithFormat:@"%@",time];
    NSTimeInterval _interval = 0;
    if (tempStr.length == 13) {
        _interval=[time longLongValue] / 1000;
    }else
    {
        _interval=[time longLongValue];
    }
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:_interval];
    NSDateFormatter *objDateformat = [[NSDateFormatter alloc] init];
    [objDateformat setDateFormat:tpye];
    return [objDateformat stringFromDate: date];
}


@end
